package com.pxene.dmp.reducer;

import java.io.IOException;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class CountDomainUserReducer extends Reducer<Text, LongWritable, LongWritable, Text> {

	private LongWritable resultKey = new LongWritable();
	private Text resultVal = new Text();
	
	@Override
	protected void setup(Context context) throws IOException, InterruptedException {
		Configuration conf = context.getConfiguration();
		// 设置配置文件名
		System.setProperty("config-file", conf.get("config-file"));
	}

	@Override
	protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException,
			InterruptedException {
		String[] columns = key.toString().split("\\|");
		if (columns.length != 4) {
			return;
		}
		String domain = columns[0];
		String imsi = columns[1];
		String province = columns[2];
		String dataType = columns[3];
		
		int sum = 0;
		for (LongWritable value : values) {
			sum += value.get();
		}
		
		resultKey.set(new Date().getTime());
		resultVal.set(domain + "|" + imsi + "|" + sum + "|" + province + "|" + dataType);
		context.write(resultKey, resultVal);
	}
	
}
